package com.ruoyi.web.controller.gcgl;

import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gcgl.domain.GcProject;
import com.ruoyi.gcgl.domain.GcProjectFlow;
import com.ruoyi.gcgl.domain.GcProjectSgazht;
import com.ruoyi.gcgl.domain.GcProjectSgazhtjgzl;
import com.ruoyi.gcgl.service.IGcProjectFlowService;
import com.ruoyi.gcgl.service.IGcProjectService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtService;
import com.ruoyi.gcgl.service.IGcProjectSgazhtjgzlService;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 施工安装合同竣工资料Controller
 * 
 * @author ruoyi
 * @date 2022-12-11
 */
@Controller
@RequestMapping("/gcgl/sgazhtjgzl")
public class GcProjectSgazhtjgzlController extends BaseController
{
    private String prefix = "gcgl/sgazhtjgzl";

    @Autowired
    private IGcProjectSgazhtjgzlService gcProjectSgazhtjgzlService;
    @Autowired
    private IGcProjectSgazhtService gcProjectSgazhtService;
    @Autowired
    private IGcProjectService gcProjectService;
    @Autowired
    private IGcProjectFlowService gcProjectFlowService;

    @RequiresPermissions("gcgl:sgazhtjgzl:view")
    @GetMapping()
    public String sgazhtjgzl()
    {
        return prefix + "/sgazhtjgzl";
    }

    /**
     * 查询施工安装合同竣工资料列表
     */
    @RequiresPermissions("gcgl:sgazhtjgzl:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcProjectSgazhtjgzl gcProjectSgazhtjgzl)
    {
        startPage();
        gcProjectSgazhtjgzl.setDeleteFlag(1);
        if(getSysUser().isShow43R(getUserId())){
        gcProjectSgazhtjgzl.setCreateUser(getLoginName());
        }
        List<GcProjectSgazhtjgzl> list = gcProjectSgazhtjgzlService.selectGcProjectSgazhtjgzlList(gcProjectSgazhtjgzl);
        return getDataTable(list);
    }

    /**
     * 导出施工安装合同竣工资料列表
     */
    @RequiresPermissions("gcgl:sgazhtjgzl:export")
    @Log(title = "施工安装合同竣工资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcProjectSgazhtjgzl gcProjectSgazhtjgzl)
    {
    	gcProjectSgazhtjgzl.setDeleteFlag(1);
        if(getSysUser().isShow()){
        gcProjectSgazhtjgzl.setCreateUser(getLoginName());
        }
        List<GcProjectSgazhtjgzl> list = gcProjectSgazhtjgzlService.selectGcProjectSgazhtjgzlList(gcProjectSgazhtjgzl);
        ExcelUtil<GcProjectSgazhtjgzl> util = new ExcelUtil<GcProjectSgazhtjgzl>(GcProjectSgazhtjgzl.class);
        return util.exportExcel(list, "施工安装合同竣工资料数据");
    }

    /**
     * 新增施工安装合同竣工资料
     */
    @GetMapping("/add/{sgazhtId}")
    public String add(@PathVariable("sgazhtId") Long sgazhtId, ModelMap mmap)
    {
    	GcProjectSgazht gcProjectSgazht = gcProjectSgazhtService.selectGcProjectSgazhtById(sgazhtId);
    	mmap.put("gcProjectid", gcProjectSgazht.getGcProjectid());
    	mmap.put("sgazhtId", sgazhtId);
    	mmap.put("orderNo", gcProjectSgazht.getOrderNo());
        return prefix + "/add";
    }

    /**
     * 新增保存施工安装合同竣工资料
     */
    @RequiresPermissions("gcgl:sgazhtjgzl:add")
    @Log(title = "施工安装合同竣工资料", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcProjectSgazhtjgzl gcProjectSgazhtjgzl,@RequestParam("zilwjfile")MultipartFile filewj){
        try{
            if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcProjectSgazhtjgzl.setZilwj(zilwj);
                gcProjectSgazhtjgzl.setDeleteFlag(1);
                gcProjectSgazhtjgzl.setCreateAt(new Date());
                gcProjectSgazhtjgzl.setUpdateAt(new Date());
                gcProjectSgazhtjgzl.setCreateUser(getLoginName());
                gcProjectSgazhtjgzl.setUpdateUser(getLoginName());
                gcProjectSgazhtjgzlService.insertGcProjectSgazhtjgzl(gcProjectSgazhtjgzl);
            }
    	}catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 修改施工安装合同竣工资料
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazhtjgzl gcProjectSgazhtjgzl = gcProjectSgazhtjgzlService.selectGcProjectSgazhtjgzlById(id);
        mmap.put("gcProjectSgazhtjgzl", gcProjectSgazhtjgzl);
        return prefix + "/edit";
    }

    /**
     * 修改保存施工安装合同竣工资料
     */
    @Log(title = "施工安装合同竣工资料", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcProjectSgazhtjgzl gcProjectSgazhtjgzl,@RequestParam("zilwjfile")MultipartFile filewj){
    	try{
            if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcProjectSgazhtjgzl.setZilwj(zilwj);
                gcProjectSgazhtjgzl.setUpdateAt(new Date());
                gcProjectSgazhtjgzl.setUpdateUser(getLoginName());
            	gcProjectSgazhtjgzlService.updateGcProjectSgazhtjgzl(gcProjectSgazhtjgzl);
            }
        }
        catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 删除施工安装合同竣工资料
     */
    @RequiresPermissions("gcgl:sgazhtjgzl:remove")
    @Log(title = "施工安装合同竣工资料", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcProjectSgazhtjgzlService.deleteGcProjectSgazhtjgzlByIds(ids));
    }
    
    //--------------
    /**
     * 新增施工安装合同竣工资料
     */
    @GetMapping("/addYs/{gcProjectid}")
    public String addYs(@PathVariable("gcProjectid") Long gcProjectid, ModelMap mmap){
    	GcProject gcProject = gcProjectService.selectGcProjectById(gcProjectid);
    	mmap.put("gcProjectid", gcProjectid);
    	mmap.put("sgazhtId", "");
    	mmap.put("orderNo", gcProject.getOrderNo());
        return prefix + "/addYs";
    }

    /**
     * 新增保存施工安装合同竣工资料
     */
    @Log(title = "施工安装合同竣工资料", businessType = BusinessType.INSERT)
    @PostMapping("/addYs")
    @ResponseBody
    public AjaxResult addYsSave(GcProjectSgazhtjgzl gcProjectSgazhtjgzl,@RequestParam("zilwjfile")MultipartFile filewj){
        try{
            if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcProjectSgazhtjgzl.setZilwj(zilwj);
                gcProjectSgazhtjgzl.setDeleteFlag(1);
                gcProjectSgazhtjgzl.setCreateAt(new Date());
                gcProjectSgazhtjgzl.setUpdateAt(new Date());
                gcProjectSgazhtjgzl.setCreateUser(getLoginName());
                gcProjectSgazhtjgzl.setUpdateUser(getLoginName());
                gcProjectSgazhtjgzlService.insertGcProjectSgazhtjgzl(gcProjectSgazhtjgzl);
            
                GcProjectFlow gcProjectFlow = new GcProjectFlow();
                gcProjectFlow.setGcProjectid(gcProjectSgazhtjgzl.getGcProjectid());
                gcProjectFlow.setShazhtjgcl(1);
                gcProjectFlowService.updateGcProjectFlow(gcProjectFlow);
            }
    	}catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }

    /**
     * 修改施工安装合同竣工资料
     */
    @GetMapping("/editYs/{id}")
    public String editYs(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcProjectSgazhtjgzl gcProjectSgazhtjgzl = gcProjectSgazhtjgzlService.selectGcProjectSgazhtjgzlById(id);
        mmap.put("gcProjectSgazhtjgzl", gcProjectSgazhtjgzl);
        return prefix + "/editYs";
    }

    /**
     * 修改保存施工安装合同竣工资料
     */
    @Log(title = "施工安装合同竣工资料", businessType = BusinessType.UPDATE)
    @PostMapping("/editYs")
    @ResponseBody
    public AjaxResult editYsSave(GcProjectSgazhtjgzl gcProjectSgazhtjgzl,@RequestParam("zilwjfile")MultipartFile filewj){
    	try{
            if (!filewj.isEmpty()){
                String zilwj = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), filewj, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
                gcProjectSgazhtjgzl.setZilwj(zilwj);
                gcProjectSgazhtjgzl.setUpdateAt(new Date());
                gcProjectSgazhtjgzl.setUpdateUser(getLoginName());
            	gcProjectSgazhtjgzlService.updateGcProjectSgazhtjgzl(gcProjectSgazhtjgzl);
            }
        }
        catch (Exception e){
            return error(e.getMessage());
        }
    	
        return toAjax(1);
    }
}